package com.maoye.ossp.biz.statement.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hotent.base.api.query.QueryFilter;
import com.hotent.base.api.query.QueryOP;
import com.maoye.ossp.biz.brand.entity.Supplier;
import com.maoye.ossp.biz.brand.service.ISupplierService;
import com.maoye.ossp.biz.statement.entity.StatementMainSelfrun;
import com.maoye.ossp.biz.statement.service.StatementMainDetail1Service;
import com.maoye.ossp.biz.statement.service.StatementMainDetail2Service;
import com.maoye.ossp.biz.statement.service.StatementMainDetail3Service;
import com.maoye.ossp.biz.statement.service.StatementMainSelfrunService;
import com.maoye.ossp.common.entity.Page;
import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.sys.org.util.UserUtils;
/**
 * 自营对账controller类
 * 
 * @author 梁涛
 * 
 */
@Controller
@RequestMapping(value = "biz/statement/mainSelfrun/")
public class StatementMainSelfrunController extends BaseController {
	@Resource
	private StatementMainSelfrunService statementMainSelfrunService;
	
	@Resource
	private StatementMainDetail1Service statementMainDetail1Service;
	
	@Resource
	private StatementMainDetail2Service statementMainDetail2Service;
	
	@Resource
	private StatementMainDetail3Service statementMainDetail3Service;
	
	@Resource
	private ISupplierService supplierService;
	
	@RequestMapping(value = "list")
	public String list(StatementMainSelfrun statementMainSelfrun,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		//获取登陆用户Id 
		Long userId=UserUtils.getCurrentUserId();
		//根据当前用户Id 如果是供应商 获取用户获取供应商信息
		Supplier supplier=supplierService.get(userId);
		QueryFilter queryFilter = getQuerFilter(request,statementMainSelfrun);	
		//设置供应商编码
		if(supplier!=null){
			queryFilter.addFilter("supplierNo", supplier.getSupplierNo(), QueryOP.EQUAL);
		 }
		Page<StatementMainSelfrun> page = (Page<StatementMainSelfrun>) statementMainSelfrunService
				.find(queryFilter);
		model.addAttribute("page", page);
		model.addAttribute("statementMainSelfrun", statementMainSelfrun);
		return resultPage("/biz/statement/statementMainSelfrun/list", model, request);
	}
	
	@RequestMapping(value = "view")
	public String view(StatementMainSelfrun statementMainSelfrun,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		Long id=Long.parseLong(request.getParameter("id"));
		String billNo=request.getParameter("billNo");
		statementMainSelfrun=statementMainSelfrunService.get(id);
		
		//获取 自营对账单-采购/调价明细
		List statementMainDetail1List=statementMainDetail1Service.getStatementMainDetail1ByBillNo(billNo);
		if(statementMainDetail1List!=null){
			model.addAttribute("statementMainDetail1List",statementMainDetail1List);
		}
		
		//获取 自营对账单-扣费明细/返利明细
		List statementMainDetail2List=statementMainDetail2Service.getStatementMainDetail2ByBillNo(billNo);
		if(statementMainDetail2List!=null){
			model.addAttribute("statementMainDetail2List",statementMainDetail2List);
		}
		
		//获取 自营对账单-销售明细
		List statementMainDetail3List=statementMainDetail3Service.getStatementMainDetail3ByBillNo(billNo);
		if(statementMainDetail3List!=null){
			model.addAttribute("statementMainDetail3List",statementMainDetail3List);
		}
		
		model.addAttribute("statementMainSelfrun", statementMainSelfrun);
		return resultPage("/biz/statement/statementMainSelfrun/view", model, request);
	}
}



